package org.example.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.pojo.BOM;
import org.example.pojo.JavaType;
import org.example.pojo.Member;
import org.example.pojo.Properties;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Java写入excel
 */
public class ExcelWriter {
    public static void main(String[] args) {
        ExcelWriter.writeXomFile(new HashMap<>(),"D:\\Study\\my-common-utils\\bom转excel");
    }

    public static void writeXomFile(Map<String,JavaType> javaTypeMap, String fileName) {
        Workbook workbook = new XSSFWorkbook();
        //创建一个sheet页
        Sheet sheet = workbook.createSheet("BOM");
        //创建表头
        createHeader(sheet);
        int rowIndex = 2;
        //创建表数据
        for(Map.Entry<String,JavaType> entry:javaTypeMap.entrySet()){
            JavaType javaType = entry.getValue();
            for(Map.Entry<String, Properties> propertyEntry:javaType.getPropertiesMap().entrySet()){
                Row row = sheet.createRow(rowIndex);
                Cell cell1 = row.createCell(0);
                cell1.setCellValue(javaType.getName());
                Cell cell2 = row.createCell(1);
                cell2.setCellValue(javaType.getDescription());
                Properties property = propertyEntry.getValue();
                Cell cell3 = row.createCell(2);
                cell3.setCellValue(property.getPhraseNavigation());
                Cell cell4 = row.createCell(3);
                cell4.setCellValue(property.getName());
                Cell cell5 = row.createCell(4);
                cell5.setCellValue(property.getStyle());
                rowIndex += 1;
            }

        }

        File file = new File(fileName,  "BOM模型.xlsx");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeWorkbook(workbook);
        }
    }

    public static void createHeader(Sheet sheet){
        Row headerRow = sheet.createRow(0);
        Cell bomCell1 = headerRow.createCell(0);
        bomCell1.setCellValue("BOM对象");
        Cell bomCell2 = headerRow.createCell(1);
        CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, 1);
        sheet.addMergedRegion(cellAddresses);
        Cell propertyCell1 = headerRow.createCell(2);
        propertyCell1.setCellValue("词条描述");
        Cell propertyCell2 = headerRow.createCell(3);
        Cell propertyCell3 = headerRow.createCell(4);
        CellRangeAddress cellAddresses2 = new CellRangeAddress(0, 0, 2, 4);
        sheet.addMergedRegion(cellAddresses2);

        Row headerRow2 = sheet.createRow(1);
        Cell row2Cell1 = headerRow2.createCell(0);
        row2Cell1.setCellValue("英文名称");
        Cell row2Cell2 = headerRow2.createCell(1);
        row2Cell2.setCellValue("中文名称");
        Cell row2Cell3 = headerRow2.createCell(2);
        row2Cell3.setCellValue("词条中文名");
        Cell row2Cell4 = headerRow2.createCell(3);
        row2Cell4.setCellValue("词条英文名");
        Cell row2Cell15 = headerRow2.createCell(4);
        row2Cell15.setCellValue("数据类型");

    }




    /**
     * BOM对象写入excel文件
     * @param bom
     * @param fileName
     */
    public static void writerFile(BOM bom, String fileName) {
        System.out.println("开始写入文件" + bom.getClassName());
        Workbook workbook = new XSSFWorkbook();
        //创建一个sheet页
        Sheet sheet = workbook.createSheet(bom.getClassName());
        // 创建第一行（标题行）
        Row headerRow = sheet.createRow(0);
        // 创建单元格并设置值
        Cell headerCell1 = headerRow.createCell(0);
        headerCell1.setCellValue("值列");

        Cell headerCell2 = headerRow.createCell(1);
        headerCell2.setCellValue("标签列");

        Cell headerCell3 = headerRow.createCell(2);
        headerCell3.setCellValue("BOM到XOM列");
        List<Row> rows = new ArrayList<>();

        //创建数据行
        for(int i=0;i<bom.getMembers().size();i++) {
            Row row = sheet.createRow(i + 1);
            Member member = bom.getMembers().get(i);
            row.createCell(0).setCellValue(member.getCode());
            row.createCell(1).setCellValue(member.getLabel());
            row.createCell(2).setCellValue(member.getXOMValue());
        }

        File file = new File(fileName, bom.getClassName() + "_" + bom.getDescription() + ".xlsx");

        try (FileOutputStream fos = new FileOutputStream(file)) {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            closeWorkbook(workbook);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    public static void test01(String[] args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        // 创建一行
        Row headerRow = sheet.createRow(0);
        // 创建单元格并设置值
        Cell headerCell1 = headerRow.createCell(0);
        headerCell1.setCellValue("Header1");

        Cell headerCell2 = headerRow.createCell(1);
        headerCell2.setCellValue("Header2");

        // 创建数据行
        Row row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("Data1");
        row1.createCell(1).setCellValue("Data2");

        // 写入文件
        try (FileOutputStream fos = new FileOutputStream("D:\\中汇日志\\工作日志\\1126测试往预生产同步\\file.xlsx")) {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭工作簿
     *
     * @param workbook
     */
    public static void closeWorkbook(Workbook workbook) {
        try {
            if (workbook != null) {
                workbook.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
